import ManageAPI from "@/api/manage";
import { store } from "@/stores";
import { defineStore } from "pinia";
import { ref } from "vue";
import axios from "axios";

export const useManageStore = defineStore("manage", () => {
  // 基本设置参数
  const settingInfo = ref({});
  // 浏览器唯一标识
  const userAgent = navigator.userAgent;

  /**
   * 获取基本设置
   */
  function getSetting() {
    return new Promise((resolve, reject) => {
      ManageAPI.getSetting()
        //   axios
        //     .get("mock/getSetting.json")
        .then((res) => {
          setSettingInfo(res.data);
          resolve(res.data);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
  // 存储基本设置
  function setSettingInfo(value) {
    settingInfo.value = value;
  }

  return {
    userAgent,
    settingInfo,
    getSetting,
    setSettingInfo,
  };
});

/**
 * 用于在组件外部（如在Pinia Store 中）使用 Pinia 提供的 store 实例。
 * 官方文档解释了如何在组件外部使用 Pinia Store：
 * https://pinia.vuejs.org/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
 */
export function useManageStoreStoreHook() {
  return useManageStore(store);
}
